package message;

import model.RoomInfo;
import socket.ChessServer;

/**
 * 准备报文.
 *
 * @author 张紘溢
 */
public class ReadyMessage extends BaseMessage {
    private int roomId;
    private boolean isLeft;

    public boolean isLeft() {
        return isLeft;
    }

    public ReadyMessage(int roomId, boolean isLeft) {
        this.roomId = roomId;
        this.isLeft = isLeft;
    }

    @Override
    public void sendToServer() {
        RoomInfo roomInfo = ChessServer.getInstance().getRooms().get(roomId);
        System.out.println("123" + roomInfo);
        if (roomInfo.isLeftReady() && roomInfo.isRightReady()) {
            return;
        }
        if (isLeft) {
            if (!roomInfo.isLeftReady()) {//如果左边处于未准备状态
                roomInfo.setLeftReady(true);
                ChessServer.getInstance().getRooms().set(roomId, roomInfo);
                System.out.println("左边准备了");
                if (roomInfo.getRightPlayer() != null) {
                    //更新对手的界面
                    ServerReadyMessage serverReadyMessage = new ServerReadyMessage(roomInfo);
                    ChessServer.getInstance().sendMsgToClient(serverReadyMessage, roomInfo.getRightPlayer());
                }
                if (roomInfo.isRightReady()) {
                    System.out.println("左边右边都准备了");
                    //向两边发送开始游戏报文
                    ServerStartGameMessage serverStartGameMessageLeft = new ServerStartGameMessage();
                    ChessServer.getInstance().sendMsgToClient(serverStartGameMessageLeft, roomInfo.getLeftPlayer());
                    ServerStartGameMessage serverStartGameMessageRight = new ServerStartGameMessage();
                    ChessServer.getInstance().sendMsgToClient(serverStartGameMessageRight, roomInfo.getRightPlayer());
                }
            } else {//如果左边处于准备状态
                System.out.println("左边取消准备了");
                roomInfo.setLeftReady(false);
                ChessServer.getInstance().getRooms().set(roomId, roomInfo);
                if (roomInfo.getRightPlayer() != null) {
                    ServerReadyMessage serverReadyMessage = new ServerReadyMessage(roomInfo);
                    ChessServer.getInstance().sendMsgToClient(serverReadyMessage, roomInfo.getRightPlayer());
                }
            }
        } else {
            if (!roomInfo.isRightReady()) {
                roomInfo.setRightReady(true);
                ChessServer.getInstance().getRooms().set(roomId, roomInfo);
                System.out.println(roomInfo.isLeftReady() + "右边准备了");
                if (null != roomInfo.getLeftPlayer()) {
                    ServerReadyMessage serverReadyMessage = new ServerReadyMessage(roomInfo);
                    ChessServer.getInstance().sendMsgToClient(serverReadyMessage, roomInfo.getLeftPlayer());
                }
                if (roomInfo.isLeftReady()) {
                    // 报文还没写完整
                    System.out.println("左边右边都准备了");
                    roomInfo.setRightReady(true);
                    System.out.println("----------------");
                    ServerStartGameMessage serverStartGameMessageLeft = new ServerStartGameMessage();
                    ChessServer.getInstance().sendMsgToClient(serverStartGameMessageLeft, roomInfo.getLeftPlayer());
                    ServerStartGameMessage serverStartGameMessageRight = new ServerStartGameMessage();
                    ChessServer.getInstance().sendMsgToClient(serverStartGameMessageRight, roomInfo.getRightPlayer());
                }
            } else {
                System.out.println("右边取消准备了");
                roomInfo.setRightReady(false);
                ChessServer.getInstance().getRooms().set(roomId, roomInfo);
                if (null != roomInfo.getLeftPlayer()) {
                    ServerReadyMessage serverReadyMessage = new ServerReadyMessage(roomInfo);
                    ChessServer.getInstance().sendMsgToClient(serverReadyMessage, roomInfo.getLeftPlayer());
                }
            }
        }
    }

    @Override
    public void selfExecute() {

    }
}